package com.hua.store.domin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Cart {
    private Map<String, CartItem> map = new HashMap<String, CartItem>();//个数不确定的购物项
    private double total;//总计，积分

    public void clearCart() {
        map.clear();
    }

    public Collection<CartItem> getCartItems() {
        return map.values();
    }

    public void removeCartItem(String pid) {
        map.remove(pid);
    }

    public void addToCart(CartItem cartItem) {
        //判断以前购物车中有没有存放要加入的商品再进行代码编写
        String pid = cartItem.getProduct().getPid();
        if (map.containsKey(pid)) {
            CartItem old = map.get(pid);
            old.setNum(old.getNum() + cartItem.getNum());
        } else {
            map.put(pid, cartItem);
        }
    }

    public Cart() {
    }

    public Cart(Map<String, CartItem> map, double total) {
        this.map = map;
        this.total = total;
    }

    public Map<String, CartItem> getMap() {
        return map;
    }

    public void setMap(Map<String, CartItem> map) {
        this.map = map;
    }

    public double getTotal() {
        total = 0;
        Collection<CartItem> values = map.values();
        for (CartItem cartItem : values) {
            total += cartItem.getSubTotal();
        }
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
}
